var t = getApp(), e = t.requirejs('core');
Page({
  data: {
    // 数据列表
    list: [],
    // 数量列表
    count: [],
    // 用于控制复选框状态
    checkbox: [],
    // 用于控制全选状态
    isall: false,
    // 用于控制按钮状态
    isdisable: true,
    search: '',
    avatar: "",
    nickname: "",
  },
  onLoad(options) {
    this.get_data();
  },
  // 修改复选框
  checkbox(params) {
    var index = params.currentTarget.dataset.index;
    var checkbox = this.data.checkbox;
    checkbox[index] = !checkbox[index];
    // 判断是否全部都为真
    if (checkbox.indexOf(false) == -1) {this.setData({isall: true})} else {this.setData({isall: false})}
    // 判断是否有真
    if (checkbox.indexOf(true) == -1) {this.setData({isdisable: true})} else {this.setData({isdisable: false})}
    this.setData({checkbox: checkbox});
  },
  // 全选
  all() {
    this.setData({isall: !this.data.isall});
    var checkbox = this.data.checkbox;
    for (let index = 0; index < checkbox.length; index++) {
      checkbox[index] = this.data.isall;
    }
    // 判断是否有真
    this.setData({checkbox: checkbox})
    if (checkbox.indexOf(true) == -1) {this.setData({isdisable: true})} else {this.setData({isdisable: false})}
  },
  // 减
  sub(params) {
    var index = params.currentTarget.dataset.index;
    let count = this.data.count;
    if (count[index] == 1) {
      wx.showToast({title: '最小一个', icon: 'none'})
      return
    } else {
      count[index] = count[index] - 1;
      this.setData({count: count})
    }
  },
  // 加
  add(params) {
    var index = params.currentTarget.dataset.index;
    let count = this.data.count;
    count[index] = count[index] + 1;
    this.setData({count: count})
  },
  // 获取数据
  get_data() {
    e.get("zstock/mystock", {}, res => {
      // console.log(res);
      this.setData({list: res.stocks});
      var count = [], checkbox = [];
      for (let index = 0; index < res.stocks.length; index++) {
        count.push(1);
        checkbox.push(false);
      }
      this.setData({count: count, checkbox: checkbox});
    })
  },
  // 提交
  submit(params) {
    var value = params.detail.value;
    let that = this;
    // 判断数据是否完整
    if (value.mobile == '') {
      wx.showToast({title: '请输入转移对象', icon: 'none'})
    } else if (value.mobile == 'name') {
      wx.showToast({title: '请输入转移姓名', icon: 'none'})
    }
    e.get("member/repertory/changeSearch", {phone: value.mobile}, res => {
      if (res.status == 1) {
        let list = that.data.list;
        console.log(list);
        let str = []
        for (let index = 0; index < list.length; index++) {
          str.push(list[value.checkbox[index]].sid + ':' + list[value.checkbox[index]].id + ":" + that.data.count[value.checkbox[index]])
        }
        e.get('zstock/mystock/stockout', {zyObject:value.mobile,str: str.join(',')}, result => {
          // console.log(result);
          if (result.status == 1) {
            wx.showToast({title: '转移成功', icon: 'none'});
            wx.navigateTo({
              url: '/pages/member/repertory/index',
            })
            that.get_data();
          } else {
            wx.showToast({title: result.result.message, icon: 'none'});
          }
        })
      } else {
        wx.showToast({title: '未找到会员', icon: 'none'})
      }
    })
  },
  // 验证会员信息
  changeSearch(params) {
    var that = this;
    var inputSearch = params.detail.value;
    // console.log(inputSearch.length);
    if(inputSearch.length>10){
      e.get('member/repertory/changeSearch', {search:inputSearch}, res => {
        // console.log(res)
        let that=this;
        if(res.status = 1){
          that.setData({
            avatar: res.result.avatar,
            nickname: res.result.nickname
          })
        }
      })
    }else{
      that.setData({
        avatar: "",
        nickname: ""
      })
    }
  }
})